<?php

class Empeval_Methods_Request_SendDecision {

	/**
	 *
	 * @var App_Methods_Data_App
	 */
	public $app;
	public $bonusGoalsFormId;
	public $mambers;
	public $date;
	public $text;

	public function __construct($params) {
		$this->app = new App_Methods_Data_App($params->app);
		$this->bonusGoalsFormId = $params->bonusGoalsFormId;
		$this->mambers = $params->getDefault('mambers');
		$this->date = $params->date;
		$this->text = $params->text;
		$this->attachment = $params->attachment;
	}

	public function process() {
		$reject = EmpevalRejectionsQuery::create()->findOneByBonusGoalsFormId($this->bonusGoalsFormId);
		$bonusGoalsForm = EmpevalViewBgfWithSuperiorPositionIdQuery::create()
		->useUserPositionsRelatedBySuperiorUserPositionIdQuery('SuperiorUserPosition')
			->joinUsers('Superior')
		->endUse()
		->with('SuperiorUserPosition')
		->with('Superior')
		->findOneById($this->bonusGoalsFormId);
		if (!($reject instanceof EmpevalRejections) || !($bonusGoalsForm instanceof EmpevalViewBgfWithSuperiorPositionId))
			throw new Empeval_Exception_App(Empeval_Exception_Codes::INFO_ACCESS_DENIED);

		$con = Propel::getConnection('vgp_empeval');
		$con->beginTransaction();
		try {
			foreach ($this->mambers as $memberId) {
				$member = new EmpevalCommittees();
				$member->setUserId((int)$memberId);
				$reject->addCommittees($member);
			}
			$reject->setDecision($this->text);
			$reject->setMeetingDate($this->date);
			$reject->setAttachment($this->attachment);
			$reject->setStatus(1);
			$reject->save($con);
			EmpevalBonusGoalsFormsQuery::create()
			->filterById($this->bonusGoalsFormId)
			->update(array('Status' => EmpevalBonusGoalsForms::STATUS_CLOSED), $con);
			
			$notification = new Empeval_Notifications_Decision();
			$notification->addNotifications($reject, $bonusGoalsForm);
			
			$con->commit();
		} catch (Exception $e) {
			$con->rollBack();
			throw $e;
		}

		return new Empeval_Methods_Response_SendDecision(true, $reject);
	}

}